﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace FinalProject
{
    public partial class wucHoSo : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Binding(0);
            }
            else
            {
                dtlHoSo.ItemCommand += new DataListCommandEventHandler(dtlHoSo_ItemCommand);
            }
        }

        void dtlHoSo_ItemCommand(object source, DataListCommandEventArgs e)
        {                       
            int index = e.Item.ItemIndex;
            switch (e.CommandName)            
            {
                case "lbtXem":
                    Response.Redirect("TaoHoSo-HoanTat.aspx?isVisibile=false&id=" + index.ToString());
                    break;
                case "lbtCapNhat":            
                    Response.Redirect("TaoHoSoThongTinCaNhan.aspx?id=" + index.ToString());
                    break;
                case "lbtXoa":
                    Utility.deleteHoSo(Utility.UserCurrent.LstHoSo[index].ID);
                    Utility.UserCurrent.LstHoSo.RemoveAt(index);
                    Binding();
                    break;
                default:
                    break;
            }
        }

        private void Binding()
        {
            try
            {
                if (Utility.UserCurrent.LstHoSo != null && Utility.UserCurrent.LstHoSo.Count > 0)
                {
                    dtlHoSo.DataSource = Utility.UserCurrent.LstHoSo;
                    dtlHoSo.DataBind();
                }
            }
            catch { }
        }

        public void Binding(int n)
        {
            try
            {
                if (Utility.UserCurrent.LstHoSo != null && Utility.UserCurrent.LstHoSo.Count > 0)
                {
                    if (n == 0)
                    {
                        dtlHoSo.DataSource = Utility.UserCurrent.LstHoSo;
                        dtlHoSo.DataBind();
                    }
                    else
                    {
                        List<HoSo> lst = new List<HoSo>();
                        for (int i = 0; i < Utility.UserCurrent.LstHoSo.Count; i++)
                        {
                            lst.Add(Utility.UserCurrent.LstHoSo[i]);
                            if (i == n - 1)
                                break;
                        }
                        dtlHoSo.DataSource = lst;
                        dtlHoSo.DataBind();
                    }
                }
            }
            catch { }
        }        
    }
}